﻿using System;
using System.Xml;

namespace XamlLocalizer.Parsing
{
    public class XStaticReplacement : IReplacementStrategy
    {
        public string Id { get { return "xStatic"; } }

        public string Name { get { return "x:Static Resource"; } }

        public override string ToString()
        {
            return Name;
        }

        public bool ExtractRootElementAttributesIntoElements { get { return false; } }

        private static string GetFullKey(ResourceKeyDesc keyDesc)
        {
            return String.Format("{0}:{1}.{2}",
                XamlExtractor.XamlParseInfo.ReplaceSettings.LocExtPrefix,
                keyDesc.ResourceFile, keyDesc.ItemKey);
        }

        public string GetAttributeReplacementValue(ResourceKeyDesc keyDesc)
        {
            return String.Format("{{x:Static {0}}}", GetFullKey(keyDesc));
        }

        public XmlElement CreateReplacementElement(XmlDocument ownerDoc, ResourceKeyDesc keyDesc)
        {
            XmlElement newElem = ownerDoc.CreateElement("Static", "http://schemas.microsoft.com/winfx/2006/xaml");

            newElem.SetAttribute("Member", GetFullKey(keyDesc));
            return newElem;
        }

        public void PrepareReplacement(XamlExtractor xamlExtractor, string resourcesNamespace, string resourceClass)
        {
            xamlExtractor.AddNamespace(XamlExtractor.XamlParseInfo.ReplaceSettings.LocExtPrefix, "clr-namespace:" + resourcesNamespace);
        }
    }
}
